package com.chenx.gameview;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.R.bool;
import android.R.integer;
import android.graphics.Bitmap;
import android.media.SoundPool;
import android.util.Log;

import com.chenx.gameinterface.GameImage;

public class DiJiImage implements GameImage
{
	private List<GameImage> gameImages;
	private int display_w, display_h;
	private int index = 0;
	private int changenum = 0;
	private boolean Gstate = false;

	private Bitmap diren = null;
	private List<Bitmap> bitmaps = new ArrayList<Bitmap>();
	private List<Bitmap> baozhas = new ArrayList<Bitmap>();

	private int x, y, width, height;

	public DiJiImage(Bitmap diren, int display_w, int display_h,
			ArrayList<GameImage> gameImages, Bitmap baozha)
	{
		this.gameImages = gameImages;
		this.display_w = display_w;
		this.display_h = display_h;

		this.diren = diren;
		bitmaps.add(Bitmap.createBitmap(diren, 0, 0, diren.getWidth() / 4,
				diren.getHeight()));
		bitmaps.add(Bitmap.createBitmap(diren, (diren.getWidth() / 4) * 1, 0,
				diren.getWidth() / 4, diren.getHeight()));
		bitmaps.add(Bitmap.createBitmap(diren, (diren.getWidth() / 4) * 2, 0,
				diren.getWidth() / 4, diren.getHeight()));
		bitmaps.add(Bitmap.createBitmap(diren, (diren.getWidth() / 4) * 3, 0,
				diren.getWidth() / 4, diren.getHeight()));

		baozhas.add(Bitmap.createBitmap(baozha, 0, 0, baozha.getWidth() / 4,
				baozha.getHeight() / 2));
		baozhas.add(Bitmap.createBitmap(baozha, (baozha.getWidth() / 4) * 1, 0,
				baozha.getWidth() / 4, baozha.getHeight() / 2));
		baozhas.add(Bitmap.createBitmap(baozha, (baozha.getWidth() / 4) * 2, 0,
				baozha.getWidth() / 4, baozha.getHeight() / 2));
		baozhas.add(Bitmap.createBitmap(baozha, (baozha.getWidth() / 4) * 3, 0,
				baozha.getWidth() / 4, baozha.getHeight() / 2));

		baozhas.add(Bitmap.createBitmap(baozha, 0, baozha.getHeight() / 2,
				baozha.getWidth() / 4, baozha.getHeight() / 2));
		baozhas.add(Bitmap.createBitmap(baozha, (baozha.getWidth() / 4) * 1,
				baozha.getHeight() / 2, baozha.getWidth() / 4,
				baozha.getHeight() / 2));
		baozhas.add(Bitmap.createBitmap(baozha, (baozha.getWidth() / 4) * 2,
				baozha.getHeight() / 2, baozha.getWidth() / 4,
				baozha.getHeight() / 2));
		baozhas.add(Bitmap.createBitmap(baozha, (baozha.getWidth() / 4) * 3,
				baozha.getHeight() / 2, baozha.getWidth() / 4,
				baozha.getHeight() / 2));

		y = -diren.getHeight();
		Random ran = new Random();
		x = ran.nextInt(display_w - (diren.getWidth() / 4));

		width = diren.getWidth() / 4;

		height = diren.getHeight();
	}

	@Override
	public Bitmap getBitmap()
	{
		Bitmap bitmap = bitmaps.get(index);

		if (changenum == 7)
		{
			index++;
			changenum = 0;
			if (index == bitmaps.size())
			{
				index = 0;
			}

			if (index == 8 && Gstate)
			{
				gameImages.remove(this);
				return null;
			}

		}
		changenum++;
		y += 30;

		if (y > display_h)
		{
			gameImages.remove(this);
		}
		return bitmap;
	}

	@Override
	public int getX()
	{
		// TODO Auto-generated method stub
		return x;
	}

	@Override
	public int getY()
	{
		// TODO Auto-generated method stub
		return y;
	}

	public int getWidth()
	{
		return width;
	}

	public int getHeight()
	{
		return height;
	}

	// 受到攻击
	@SuppressWarnings("unchecked")
	public boolean ShouDaoGongJi(ArrayList<ZiDanImage> zidans, SoundPool pool,
			int sound)
	{
		if (!Gstate)
		{
			for (GameImage zidan : (List<GameImage>) zidans.clone())
			{
				if (zidan.getX() > x && zidan.getY() > y
						&& zidan.getX() < x + width
						&& zidan.getY() < y + height)
				{
				//	zidans.remove(zidan);
					Gstate = true;
					bitmaps = baozhas;
					pool.play(sound, 1, 1, 1, 0, 1);
					return true;
				
					// Log.i("APP.TAG", "击中");
				}
			}
		}
		return false;
	}
}
